#! /usr/bin/env python
#coding=utf-8
from uliweb.orm import *
from uliweb.utils.common import get_var
import datetime
from sqlalchemy.sql import not_

def get_modified_user():
    from uliweb import request
    
    return request.user.id

def get_values(model, ids, condition=None):
    from uliweb.orm import get_model
    model = get_model(model)
    return model.all().filter(condition).values(*ids)

class ProcessSheet(Model):
    __verbose_name__ = u'处理单表'
    
    requirement = Reference('requirement', verbose_name="准开发需求")
    type = Field(CHAR, max_length=1, verbose_name="类型", choices=get_var('PARA/COORDINATION_TYPE'))
    title = Field(str, verbose_name='处理单名称', max_length=255, required=True)
    sender = Reference('kfzx', verbose_name='提出单位', collection_name='sender_process_sheets')
#    receivers = manytomany('kfzx', verbose_name='处理单位', collection_name='receiver_process_sheets')
#    description = Field(TEXT, verbose_name='描述')
#    opinion = Field(TEXT, verbose_name='结论')
    status = Field(CHAR, verbose_name='处理单状态', max_length=1, required=True, choices=get_var('PARA/ASSE_STATUS'))
    creator = Reference('user', verbose_name='创建人', collection_name='creator_process_sheets')
    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user, auto=True, auto_add=True, collection_name='modified_user_process_sheets')
    created_date = Field(datetime.date, verbose_name='创建时间', auto_now_add=True)
    finish_date = Field(datetime.date, verbose_name='完成时间', auto_now_add=True)
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)

    def __unicode__(self):
        return self.title
    
    def __str__(self):
        return self.title

    
class ProcessSheetDetails(Model):
    __verbose_name__ = u'处理单过程表'
    
    process = Reference('processsheet', verbose_name="处理单")
    executer_depart = Reference('kfzx', verbose_name='执行单位')
    executer = Reference('kfzx', verbose_name='执行人')    #两个字段只
    sender_id = Selfreference(verbose_name='上一站ID')
    description = Field(TEXT, verbose_name='描述')
    opinion = Field(TEXT, verbose_name='结论')
    created_date = Field(datetime.date, verbose_name='开始时间', auto_now_add=True)
    plan_finish_date = Field(datetime.date, verbose_name='计划完成时间')
    finish_date = Field(datetime.date, verbose_name='实际完成时间')
    status = Field(CHAR, verbose_name='提交状态', max_length=1, required=True, choices=get_var('PARA/PROCESSSHEET_STATUS'))
    type = Field(CHAR, verbose_name='处理类型', max_length=1, required=True, choices=get_var('PARA/PROCESSSHEET_TYPE'))
#    sender = Reference('user', verbose_name='上一站处理人')
    receivers_depart = manytomany('kfzx', verbose_name='接收单位', collection_name='receivers_depart_process_sheet_details')
    receivers = manytomany('user', verbose_name='接收人', collection_name='receivers_process_sheet_details')
    modified_user = Reference('user', verbose_name='评估人', default=get_modified_user, auto=True, auto_add=True)
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    attachment = manytomany('processsheetattachment', verbose_name='附件')

class ProcessSheetAttachment(Model):
    __verbose_name__ = u'处理单过程附件'
    
    filepath = Field(FILE, verbose_name='附件路径', hint='文件大小不能超过2M，请注意文件大小')
    filename  = Field(str, verbose_name='附件名称', max_length=255)
    created_date = Field(datetime.datetime, verbose_name='创建时间', auto_now_add=True)
    modified_user = Reference('user', verbose_name='评估人', default=get_modified_user, auto=True, auto_add=True)
    modified_date = Field(datetime.datetime, verbose_name='评估时间', auto_now=True, auto_now_add=True)
    deleted = Field(bool, verbose_name='删除标志', default=False)